package com.fahai.web.service.impl;

import com.fahai.common.Constant;
import com.fahai.common.PageVo;
import com.fahai.web.dao.AllConfigDao;
import com.fahai.web.dao.CallManageDao;
import com.fahai.web.entity.AllConfig;
import com.fahai.web.service.CallRecordService;
import com.fahai.web.vo.CallFlownoVo;
import com.fahai.web.vo.CallRecordVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sky on 17/5/19.
 * 查看通话记录的列表
 */
@Service
public class CallRecordServiceImpl implements CallRecordService {

    @Autowired
    CallManageDao callManageDao;
    @Autowired
    AllConfigDao dao_config;


    /**
     * 查看律师通话详情列表
     *
     * @param callFlownoVo
     * @return
     */
    @Override
    public PageVo findCallFlowList(CallFlownoVo callFlownoVo, PageVo pageVo) {
        Page<CallFlownoVo> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        List<CallFlownoVo> callFlownoVoList = callManageDao.selectRecordListByLawyerId(callFlownoVo);
        AllConfig allConfig = dao_config.selectByPrimaryKey(2);
        String l = allConfig.getConfValue();
        for (CallFlownoVo v : callFlownoVoList
                ) {
            if (v.getCalledDuration() > Integer.parseInt(l)) {
                v.setYzth("优质");
            } else {
                v.setYzth("普通");
            }

        }
        pageVo.setResult(callFlownoVoList);
        pageVo.setTotal(  Long.valueOf(callManageDao.selectRecordCount(callFlownoVo)));
        return pageVo;
    }


    /**
     * 查看律师通话详情列表
     *
     * @param callFlownoVo
     * @return
     */
    @Override
    public PageVo findCallFlowList2(CallFlownoVo callFlownoVo, PageVo pageVo) {
        Page<CallFlownoVo> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        List<CallFlownoVo> callFlownoVoList = callManageDao.selectRecordListByLawyerIdFor(callFlownoVo);
        for (CallFlownoVo v:
callFlownoVoList       ) {
            if(null!=v.getUname()&&!"".equals(v.getUname())){
                try {
                    v.setUname(URLDecoder.decode(v.getUname(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        pageVo.setResult(callFlownoVoList);
      //  pageVo.setTotal(  Long.valueOf(callManageDao.selectRecordCountFor(callFlownoVo)));
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }


    //查询和统计行数通用解析前台传入查询条件
    private  CallRecordVo parseConditionVO(CallRecordVo callRecordVo){

        if(null!=callRecordVo.getStartTimeTo()&&callRecordVo.getStartTimeTo()!=""){
            callRecordVo.setStartTimeTo(callRecordVo.getStartTimeTo()+" 23:59:59");
        }


        return callRecordVo;
    }

    /**
     * 查看通话记录列表
     *
     * @param callRecordVo
     * @param pageVo
     * @return
     */
    public PageVo findCallRecordList(CallRecordVo callRecordVo, PageVo pageVo) {
        AllConfig vo = new AllConfig();
        vo.setConfEname(Constant.GOOD_CALL);
        List<AllConfig> vos = dao_config.select(vo);
        if (null != vos && vos.size() >= 1) {
            vo = vos.get(0);
        }
        String confValue = vo.getConfValue();//优质通话对应时长
        String yzth = callRecordVo.getYzth();
        if (yzth != null && "good".equals(yzth)) {
            callRecordVo.setCalledDuration(Integer.parseInt(confValue));
        }
       callRecordVo= parseConditionVO(callRecordVo);
        Page<CallRecordVo> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        callRecordVo.setYzth(null);

        List<CallRecordVo> callRecordVoList = callManageDao.selectCallManageList(callRecordVo);
        for (CallRecordVo v : callRecordVoList
                ) {
            try {
                if(null!=v.getUname()&&!"".equals(v.getUname())){
                    v.setUname(URLDecoder.decode(v.getUname(),"utf-8"));
                }else{
                    v.setUname("");
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (v.getCalledDuration() >=Integer.parseInt(confValue)) {
                v.setYzth("优质");
            } else {
                v.setYzth("普通");
            }

        }
        pageVo.setResult(callRecordVoList);
        return pageVo;
    }

    @Override
    public int getCount(CallRecordVo callRecordVo, PageVo pageVo) {
        AllConfig allConfig = dao_config.selectByPrimaryKey(2);
        String l = allConfig.getConfValue();
        String yzth = callRecordVo.getYzth();
        if (yzth != null && yzth.equals("good")) {
            callRecordVo.setCalledDuration(Integer.parseInt(l));
        }
        callRecordVo= parseConditionVO(callRecordVo);

        int count = callManageDao.getCount(callRecordVo);
        return count;
    }

    @Override
    public Map<String, List<Map<String, Object>>> selectChartForHours(String createTime) {

        Map<String, List<Map<String, Object>>> mp = new HashMap<String, List<Map<String, Object>>>();

        if (createTime != null && !"".equals(createTime)) {

            if (createTime.length() == 4) {

                mp.put("0", callManageDao.selectChartForMonths(createTime, 0));
                mp.put("1", callManageDao.selectChartForMonths(createTime, 1));
                return mp;

            } else if (createTime.length() == 7) {

                mp.put("0", callManageDao.selectChartForDays(createTime, 0));
                mp.put("1", callManageDao.selectChartForDays(createTime, 1));
                return mp;

            }

        }
        mp.put("0", callManageDao.selectChartForHours(createTime, 0));
        mp.put("1", callManageDao.selectChartForHours(createTime, 1));

        return mp;
    }
}
